import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useTagsBarStore = defineStore('tagsBarStore', () => {
  const visitedRoutes = ref([]) // 标签栏
  const excludeRoutes = ref([]) // 当前不显示的路由

  /**
   * @description 添加访问过的路由
   * @param {*} route
   * @returns
   */
  function addVisitedRoute(route) {
    let target = visitedRoutes.value.find((item) => item.path === route.path)
    if (target) {
      if (route.fullPath !== target.fullPath) Object.assign(target, route)
      return
    }
    let indexOf = excludeRoutes.value.indexOf(route.name)
    if (indexOf != -1) {
      excludeRoutes.value.splice(indexOf, 1)
    }
    visitedRoutes.value.push(Object.assign({}, route))
  }

  /**
   * @description 清除访问过的路由
   * @param {*} route
   */
  function delVisitedRoute(route) {
    visitedRoutes.value.forEach((item, index) => {
      if (item.path === route.path) visitedRoutes.value.splice(index, 1)
    })
    let indexOf = excludeRoutes.value.indexOf(route.name)
    if (indexOf == -1) {
      excludeRoutes.value.push(route.name)
    }
  }

  /**
   * @description 删除当前标签页以外其它全部多标签页
   * @param {*} route
   * @returns
   */
  function delOthersVisitedRoutes(route) {
    visitedRoutes.value = visitedRoutes.value.filter((item) => {
      if (excludeRoutes.value.indexOf(item.name) == -1 && !item.meta.affix && item.path !== route.path) {
        excludeRoutes.value.push(item.name)
      }
      return item.meta.affix || item.path === route.path
    })
  }
  /**
   * @description 删除当前标签页左边全部多标签页
   * @param {*} route
   * @returns
   */
  function delLeftVisitedRoutes(route) {
    let index = visitedRoutes.value.length
    visitedRoutes.value = visitedRoutes.value.filter((item) => {
      if (item.name === route.name) index = visitedRoutes.value.indexOf(item)
      if (!item.meta.affix && index > visitedRoutes.value.indexOf(item)) {
        excludeRoutes.value.push(item.name)
      }
      return item.meta.affix || index <= visitedRoutes.value.indexOf(item)
    })
  }
  /**
   * @description 删除当前标签页右边全部多标签页
   * @param {*} route
   * @returns
   */
  function delRightVisitedRoutes(route) {
    let index = visitedRoutes.value.length
    visitedRoutes.value = visitedRoutes.value.filter((item) => {
      if (item.name === route.name) index = visitedRoutes.value.indexOf(item)
      if (!item.meta.affix && index < visitedRoutes.value.indexOf(item)) {
        excludeRoutes.value.push(item.name)
      }
      return item.meta.affix || index >= visitedRoutes.value.indexOf(item)
    })
  }

  function delAllVisitedRoutes() {
    visitedRoutes.value = visitedRoutes.value.filter((item) => {
      if (excludeRoutes.value.indexOf(item.name) == -1 && !item.meta.affix) {
        excludeRoutes.value.push(item.name)
      }
      return item.meta.affix
    })
  }
  return {
    visitedRoutes,
    excludeRoutes,
    addVisitedRoute,
    delVisitedRoute,
    delOthersVisitedRoutes,
    delLeftVisitedRoutes,
    delRightVisitedRoutes,
    delAllVisitedRoutes,
  }
})
